package client.comm.baoding.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.baoding.R
import client.comm.baoding.api.bean.MeCoupon
import client.comm.baoding.databinding.LayoutCouponitemBinding
import com.bumptech.glide.RequestManager

class CouponAdapter : BaseLoadmoreAdapter<MeCoupon.Coupons> {

    val manager: RequestManager
    var limit = 0.0
    var isSelect = false

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.manager = manager
    }

    override fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_couponitem,
                parent,
                false
            )
        )
    }

    override fun bindVh(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as LayoutCouponitemBinding
        val bean = getItem(position) as MeCoupon.Coupons
        binding.bean = bean
        binding.event = this
        when (bean.status) {
            0 -> {
                if (limit >= bean.price || !isSelect)
                    binding.stateBtn.isEnabled = true
                else
                    binding.stateBtn.isEnabled = false
                binding.stateBtn.text = "立即使用"
            }
            1 -> {
                binding.stateBtn.isEnabled = false
                binding.stateBtn.text = "已使用"
            }
            2 -> {
                binding.stateBtn.isEnabled = false
                binding.stateBtn.text = "已过期"
            }
        }
    }

    var lisy: ((bean: MeCoupon.Coupons) -> Unit)? = null

    fun use(bean: MeCoupon.Coupons) {
        lisy?.invoke(bean)
    }
}